package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.AddTrainTimesActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.utils.BottomSheetUtil;
import com.eqdd.yiqidian.utils.ClickUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-1-11  下午1:42.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class AddTrainTimesActivity extends BaseActivity {

    private AddTrainTimesActivityCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;
    private BottomSheetUtil.TimeWrapper timePicker;
    private boolean isAdd;
    private int clickPos;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_train_times);
        initTopTitleBar(View.VISIBLE, "培训时间");
        initTopRightText("确定", v -> {
            if (slimAdapterEx.getData().size() > 0) {
                ArrayList<RangeTimeBean> resultBeans = new ArrayList<>();
                Flowable.fromIterable(slimAdapterEx.getData())
                        .filter(o -> o instanceof RangeTimeBean)
                        .subscribe(o -> resultBeans.add((RangeTimeBean) o));
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(Config.LIST, resultBeans);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showShort("还未选择时间段");
            }
        });
    }

    @Override
    public void initData() {

        ArrayList<Object> data = new ArrayList<>();
        ArrayList<RangeTimeBean> rangeTimeBeans = getIntent().getParcelableArrayListExtra(Config.LIST);
        if (rangeTimeBeans != null) {
            Flowable.fromIterable(rangeTimeBeans)
                    .subscribe(data::add);
        }
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_98, new SlimInjector<RangeTimeBean>() {
            @Override
            public void onInject(RangeTimeBean data, IViewInjector injector) {
                injector.text(R.id.tv_date, data.getDate())
                        .text(R.id.tv_from_time, data.getFrom())
                        .text(R.id.tv_to_time, data.getTo());
            }
        }).attachTo(dataBinding.recyclerView).updateData(data);
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            menuBridge.closeMenu();
            if (slimAdapterEx.getData().size() == 12) {
                dataBinding.includeBt.btAdd.setVisibility(View.VISIBLE);
            }
            slimAdapterEx.getData().remove(pos);
            slimAdapterEx.notifyItemRemoved(pos);
        });
        dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
            isAdd = false;
            clickPos = position;
            RangeTimeBean dataItem = (RangeTimeBean) slimAdapterEx.getDataItem(position);
            System.out.println(dataItem);
            try {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = sf.parse(dataItem.date);
                updateTime(parse.getTime(), dataItem.getFrom(), dataItem.getTo());
            } catch (ParseException e) {
                e.printStackTrace();
            }

        });
        ClickUtil.click(dataBinding.includeBt.btAdd, () -> {
            isAdd = true;
            updateTime(System.currentTimeMillis(), "09:00", "18:00");
        });

    }

    private void updateTime(long dateTime, String from, String to) {
        if (timePicker == null) {
            timePicker = BottomSheetUtil.getTimePicker(AddTrainTimesActivity.this, "选择时间", (date, fromTime, toTime) -> {
                if (isAdd) {
                    int index = slimAdapterEx.getData().size();
                    slimAdapterEx.getData().add(index, new RangeTimeBean(date, fromTime, toTime));
                    slimAdapterEx.notifyItemInserted(index);
                    if (slimAdapterEx.getData().size() == 12) {
                        dataBinding.includeBt.btAdd.setVisibility(View.GONE);
                    }
                } else {
                    ((RangeTimeBean) slimAdapterEx.getDataItem(clickPos)).date = date;
                    ((RangeTimeBean) slimAdapterEx.getDataItem(clickPos)).from = fromTime;
                    ((RangeTimeBean) slimAdapterEx.getDataItem(clickPos)).to = toTime;
                    slimAdapterEx.notifyItemChanged(clickPos);
                }
            });
        }
        timePicker.updateTime(dateTime, from, to);
        timePicker.show();
    }

    @Override
    public void setView() {

    }

    public static class RangeTimeBean implements Parcelable {
        String date;
        String from;
        String to;

        public RangeTimeBean(String date, String from, String to) {
            this.date = date;
            this.from = from;
            this.to = to;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getFrom() {
            return from;
        }

        public void setFrom(String from) {
            this.from = from;
        }

        public String getTo() {
            return to;
        }

        public void setTo(String to) {
            this.to = to;
        }

        public RangeTimeBean() {
        }

        @Override
        public String toString() {
            return "RangeTimeBean{" +
                    "date='" + date + '\'' +
                    ", from='" + from + '\'' +
                    ", to='" + to + '\'' +
                    '}';
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.date);
            dest.writeString(this.from);
            dest.writeString(this.to);
        }

        protected RangeTimeBean(Parcel in) {
            this.date = in.readString();
            this.from = in.readString();
            this.to = in.readString();
        }

        public static final Creator<RangeTimeBean> CREATOR = new Creator<RangeTimeBean>() {
            @Override
            public RangeTimeBean createFromParcel(Parcel source) {
                return new RangeTimeBean(source);
            }

            @Override
            public RangeTimeBean[] newArray(int size) {
                return new RangeTimeBean[size];
            }
        };
    }
}
